package com.corpgovernment.organization.service.impl;

import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.organization.entity.SyncEmployeeCallBackRequestEntity;
import com.corpgovernment.organization.entity.db.MbImportExcelRecord;
import com.corpgovernment.organization.entity.db.MbImportExcelRecordDetailLog;
import com.corpgovernment.organization.enumm.MbImportExcelRecordStatusEnum;
import com.corpgovernment.organization.enumm.MbImportExcelRecordTypeEnum;
import com.corpgovernment.organization.service.IMbImportExcelRecordService;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zl.cui
 * @date 2023/9/26
 */
@Component
public class RecordService {
    @Autowired
    private IMbImportExcelRecordService mbImportExcelRecordService;
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void recordProblems(BaseUserInfo baseUserInfo, List<SyncEmployeeCallBackRequestEntity.FailInfo> failInfoList, int totalCount, long recordId) {
        String status = MbImportExcelRecordStatusEnum.E.getDealCode();
        if (failInfoList.size() > 0){
            status = MbImportExcelRecordStatusEnum.F.getDealCode();
        }
        MbImportExcelRecord importExcelRecord = new MbImportExcelRecord();
        importExcelRecord.setId(recordId);
        importExcelRecord.setUid(baseUserInfo.getUid());
        importExcelRecord.setTotalCount(totalCount);
        importExcelRecord.setStatus(status);
        if (CollectionUtils.isEmpty(failInfoList)){
            importExcelRecord.setCheckPassedCount(totalCount);
            importExcelRecord.setFailCount(0);
        } else {
            int mainFailedNum = (int) failInfoList.stream().map(SyncEmployeeCallBackRequestEntity.FailInfo::getMainLineNo).distinct().count();
            int failedNum = failInfoList.size(); // 产品要求以总错误数输出
            importExcelRecord.setCheckPassedCount(Math.max(totalCount - mainFailedNum, 0));
            importExcelRecord.setFailCount(failedNum);
        }
        mbImportExcelRecordService.updateRecord(importExcelRecord);
        if (CollectionUtils.isNotEmpty(failInfoList)) {
            for (SyncEmployeeCallBackRequestEntity.FailInfo failInfo : failInfoList) {
                MbImportExcelRecordDetailLog detailLog = new MbImportExcelRecordDetailLog();
                detailLog.setRecordId(recordId);
                detailLog.setSheetIndex(failInfo.getSheetIndex());
                detailLog.setSheetName(failInfo.getSheetName());
                detailLog.setLineNo(failInfo.getLineNo());
                detailLog.setMessage(failInfo.getFailReason());
                detailLog.setStatus(MbImportExcelRecordStatusEnum.F.getDealCode());
                detailLog.setRecordId(recordId);
                mbImportExcelRecordService.insertRecordDetailLog(detailLog);
            }
        }
    }

    /**
     * 该方法不需要在事务中执行
     */
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public MbImportExcelRecord insertRecord(BaseUserInfo baseUserInfo ,String fileName,String fileUrl,int size){
        MbImportExcelRecord mbImportExcelRecord = new MbImportExcelRecord();
        mbImportExcelRecord.setUid(baseUserInfo.getUid());
        mbImportExcelRecord.setOrgId(baseUserInfo.getOrgId());
        mbImportExcelRecord.setCorpId(baseUserInfo.getBgUser() ? null : baseUserInfo.getCorpId());
        mbImportExcelRecord.setFileName(fileName);
        mbImportExcelRecord.setFileUrl(fileUrl);
        mbImportExcelRecord.setStatus(MbImportExcelRecordStatusEnum.U.getDealCode());
        mbImportExcelRecord.setType(MbImportExcelRecordTypeEnum.EMP.getCode());
        mbImportExcelRecord.setDatachangeCreatetime(new Date());
        mbImportExcelRecord.setTotalCount(size);
        long recordId = mbImportExcelRecordService.insertRecord(mbImportExcelRecord);
        mbImportExcelRecord.setId(recordId);
        return mbImportExcelRecord;
    }


    /**
     * 该方法不需要在事务中执行 -- org
     */
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public MbImportExcelRecord insertOrgRecord(BaseUserInfo baseUserInfo ,String fileName,String fileUrl,int size){
        MbImportExcelRecord mbImportExcelRecord = new MbImportExcelRecord();
        mbImportExcelRecord.setUid(baseUserInfo.getUid());
        mbImportExcelRecord.setOrgId(baseUserInfo.getOrgId());
        mbImportExcelRecord.setCorpId(baseUserInfo.getBgUser() ? null : baseUserInfo.getCorpId());
        mbImportExcelRecord.setFileName(fileName);
        mbImportExcelRecord.setFileUrl(fileUrl);
        mbImportExcelRecord.setStatus(MbImportExcelRecordStatusEnum.U.getDealCode());
        mbImportExcelRecord.setType(MbImportExcelRecordTypeEnum.ORG.getCode());
        mbImportExcelRecord.setDatachangeCreatetime(new Date());
        mbImportExcelRecord.setTotalCount(size);
        long recordId = mbImportExcelRecordService.insertRecord(mbImportExcelRecord);
        mbImportExcelRecord.setId(recordId);
        return mbImportExcelRecord;
    }

}
